package com.xiaolin.spring.validate;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @ClassName PersonValidator
 * @Description Person 校验器,只能校验Person类
 * @Detail detail
 * @Author MyPC
 * @Date 2020/11/8
 * @Version 1.0
 */
public class PersonValidator implements Validator {

    /**
     * @param aClass
     * @return
     */
    @Override
    public boolean supports(Class<?> aClass) {
        return Person.class.equals(aClass);
    }

    @Override
    public void validate(Object o, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors,"name","名称不能为空");
        Person person=(Person)o;
        if(person.getAge() < 1){
            errors.rejectValue("age","年龄无效");
        }else if(person.getAge()>110){
            errors.rejectValue("age","年龄太大了");
        }
    }
}
